package com.miyu.system.controller.rule;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.system.pojo.system.SysRule;
import com.miyu.system.pojo.system.SysRuleModel;
import com.miyu.system.service.rule.SysRuleService;
import com.miyu.system.utils.enumeration.R;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 取值规则控制器
 * @author miyu
 */

@RestController
@RequestMapping("sys-rule")
@RequiredArgsConstructor
public class SysRuleController {

    private final SysRuleService ruleService;

    /**
     * 系统规则编辑
     * @param params 参数个数
     * @return {@link JSONObject}
     */
    @RequestMapping("sysRuleEdit")
    public JSONObject sysRuleEdit(@RequestBody JSONObject params){
        SysRule sysRule =  JSONObject.parseObject(params.toString(), SysRule.class);
        sysRule =  ruleService.sysRuleEdit(sysRule);
        return R.result("101");
    }

    /**
     * 查询规则
     * @param param 规则名称
     * @return {@link JSONArray}
     */
    @RequestMapping( "querySysRule")
    public Page<SysRule> querySysRule(@RequestBody JSONObject param) {
        return ruleService.querySysRule(param);
    }

    /**
     * 删除系统规则
     * @param params 参数
     * @return {@link JSONObject}
     */
    @RequestMapping(value = "delSysRule")
    public JSONObject delSysRule(@RequestBody JSONArray params) {
        return ruleService.delSysRule(params);
    }


    /**
     * 缓存物系统规则
     * @param ruleId 参数
     * @return {@link SysRule}
     */
    @RequestMapping(value = "getSysRuleById",method = RequestMethod.GET)
    public SysRule getSysRuleById(@RequestParam String ruleId) {
        return ruleService.getSysRuleById(Integer.parseInt(ruleId));
    }

    /**
     * 查询规则模型通过密码
     * @param params 型号代码
     * @return SysRuleModel
     */
    @RequestMapping(value = "getRuleModelByCode")
    public SysRuleModel queryRuleModelByCode(@RequestBody JSONObject params) {
        String modelCode="book";
        if(params.containsKey("modelCode")){
            modelCode = params.getString("modelCode");
        }
        return ruleService.queryRuleModelByCode(modelCode);
    }


}
